#include "mq_connection.hpp"

int main()
{
    //实例化异步工作线程
    castlemq::AsyncWorker::ptr awp = std::make_shared<castlemq::AsyncWorker>();

    //实例化连接对象
    castlemq::Connection::ptr conn = std::make_shared<castlemq::Connection>("127.0.0.1",8085,awp);

    //通过连接创建信道
    castlemq::Channel::ptr channel = conn->openChannel();

    //通过信道提供的服务完成所需
    //1.声明一个exchange1，广播
    google::protobuf::Map<std::string, std::string> tmp_map;
    channel->declareExchange("exchange1",castlemq::ExchangeType::TOPIC,true,false,tmp_map);
    printf("声明交换机\n");
    //2.声明queue1，和queue2
    channel->declareQueue("queue1",true,false,false,tmp_map);
    printf("声明队列1\n");

    channel->declareQueue("queue2",true,false,false,tmp_map);
    printf("声明队列2\n");

    //3.queue1-exchange1  binding-key为queue1
    channel->queueBind("exchange1","queue1","queue1");
    printf("bind1\n");
    //4.queue2-exchange1  binding-key为news.music.#
    channel->queueBind("exchange1","queue2","news.music.#");
    printf("bind2\n");


    //循环向交换机发布消息
    printf("开始发布消息\n");
    for(int i=0;i<10;++i)
    {
        castlemq::BasicProperties bp;
        bp.set_id(castlemq::UuidHelper::Mkuuid());
        bp.set_delivery_mode(castlemq::DeliveryMode::DURABLE);
        bp.set_routing_key("news.music.pop");
        channel->basicPublish("exchange1",&bp,"Hello world"+std::to_string(i));
    }
    castlemq::BasicProperties bp;
    bp.set_id(castlemq::UuidHelper::Mkuuid());
    bp.set_delivery_mode(castlemq::DeliveryMode::DURABLE);
    bp.set_routing_key("news.music.sport");
    channel->basicPublish("exchange1",&bp,"Hello music");

    bp.set_id(castlemq::UuidHelper::Mkuuid());
    bp.set_delivery_mode(castlemq::DeliveryMode::DURABLE);
    bp.set_routing_key("news.sport");
    channel->basicPublish("exchange1",&bp,"Hello sport");

    printf("发布结束");
    //关闭信道
    conn->closeChannel(channel);
    //printf("关闭信道\n");
    return 0;
}